import Request from "@/sdk/pocky-request/index.js"; //引入
import md5 from "js-md5";
/**
 * store 子模块
 */
const wxAd = {
	// 状态
	state: {
		accessToken: '',
		userActionId: 0,
		gdt_vid: '', //广告traceid
		weixinadinfo: '', //广告adinfo
		aid: ''
	},
	// 内部派生
	getters: {

	},
	// 同步方法 commit
	mutations: {
		accessToken: (state, payload) => {
			state.accessToken = payload
		},
		userActionId: (state, payload) => {
			state.userActionId = payload
		},
		gdt_vid: (state, payload) => {
			state.gdt_vid = payload
		},
		weixinadinfo: (state, payload) => {
			state.weixinadinfo = payload
		},
		aid: (state, payload) => {
			state.aid = payload
		}
	},
	// 异步方法 dispatch
	actions: {
		/**
		 * @description  创建行为数据源ID
		 * @param {Object} context
		 * @param {Object} _data
		 */
		createActionId(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: 'https://api.weixin.qq.com/marketing/user_action_sets/add?version=v1.0&access_token=' + context.state.accessToken,
					method: 'post',
					data: _data
				}).then(res => {
					
					resolve();
				})
			})
		},
		/**
		 * @description  获取广告clickid
		 * @param {Object} context
		 */
		getClickId(context, _data) {
			console.log(_data);
			if (_data.gdt_vid) {
				this.commit("gdt_vid", _data.gdt_vid)
			}
			if (_data.weixinadinfo) {
				this.commit('weixinadinfo', _data.weixinadinfo)
				this.commit("aid", _data.weixinadinfo.split('.')[0])
			}

		},

		/**
		 * @description 获取微信公众号token
		 * @param {Object} context
		 */
		getToken(context) {
			return new Promise(resolve => {
				let _now = new Date();
				// if (uni.getStorageSync('wxToken')) { //缓存中有token
				// 	let wxToken = uni.getStorageSync('wxToken');
				// 	console.log(_now.getTime() - wxToken.time);
				// 	if ((_now.getTime() - wxToken.time) > (1000 * 7000)) { //超过2小时
				// 		Request().request({
				// 			url: 'http://mp.7637.com/wx/accToken.do',
				// 			method: 'post',
				// 			data: {}
				// 		}).then(res => {
				// 			this.commit("accessToken", res.data.data.access_token);
				// 			uni.setStorageSync('wxToken', {
				// 				tokenVal: res.data.data.access_token,
				// 				time: _now.getTime()
				// 			})
				// 			resolve();
				// 		})
				// 	} else { //没有超过两小时
				// 		this.commit("accessToken", wxToken.tokenVal);
				// 		resolve();
				// 	}
				// } else { //没有缓存
				Request().request({
					url: 'http://mp.7637.com/wx/accToken.do',
					method: 'post',
					data: {}
				}).then(res => {
					console.log(res);
					this.commit("accessToken", res.data.data.access_token);
					uni.setStorageSync('wxToken', {
						tokenVal: res.data.data.access_token,
						time: _now.getTime()
					})
					resolve();
				})
				// }
			})
		},

		/**
		 * @description 上报行为
		 * @param {Object} context
		 * @param {Object} _data
		 */
		reportAction(context, _data) {
			return new Promise(resolve => {
				if(context.state.gdt_vid){
					let _now = new Date();
					let _postData = {
						user_action_set_id: 1110479711,
						actions: [{
							url: 'http://www.qq.com',
							action_time: parseInt(_now.getTime() / 1000),
							action_type: _data.acType,
							trace: {
								click_id: context.state.gdt_vid
							}
						}]
					}
					console.log(_postData);
					switch (_data.acType) {
						case 'COMPLETE_ORDER':
							{
								_postData.actions[0].action_param = {
									value: _data.chaseValue
								}
					
							}
							break;
						case 'RESERVATION':
							{
					
							}
							break;
						case 'CONFIRM_EFFECTIVE_LEADS':
							{
					
							}
							break;
						default:
							break;
					}
					console.log(_postData);
					Request().request({
						url: 'https://api.weixin.qq.com/marketing/user_actions/add?version=v1.0&access_token=' + context.state.accessToken,
						method: 'post',
						data: _postData
					}).then(res => {
						console.log(res);
					
					}).catch(err => {
						console.log(err)
					})
				}
				
				
			})
		}
	}
}

export default wxAd
